import { defineStore } from 'pinia'
import { decryptJWT } from "@/utils/tokenUtil"
import MenuInter from "../type/menu-inter.ts";

export const useUserStore = defineStore('user', {
    state: () =>{
      return {
        username:"",
        email:"",
        phone:"",
      }
    },
    getters: {
        getUserInfo(state){
            return {
                username: state.username,
                email: state.email,
                phone: state.phone,
            }
        },
        
  },
   actions: {
    login(username:string, email:string, phone:string){
      this.username = username;
      this.email = email;
      this.phone = phone;
    }, 
       logout(){
            this.username = "";
            this.email = "";
            this.phone = "";
        }
  },
  persist: true,
  })
export const useTokenStore = defineStore('token', {
    state: () =>{
        return {
            token: "",
        }
    },
    getters: {
        getToken(state){
            return state.token
        },
        transitionToken(state){
            return decryptJWT(state.token)
        }
    },
    actions: {
        login(token:string){
            this.token = token;
        },
        logout(){
            this.token = "";
        }
    },
    persist: true,
})

export const useMenuStore = defineStore('menu', {
    state: () =>{
        return {
            menu:[],
        }
    },
    getters: {
        getMenu(state){
            if (state.menu.length > 0){
                return state.menu
            }
           return []
        }
    },
    actions: {
        setMenu(menuList:MenuInter){
            this.menu.push(...menuList)
        },
    },
})
